const vm = new Vue({
	el: '#all-box',
	data: {
		login: {
			username: '',
			password: ''
		},
		register: {
			username: '',
			password: '',
			rePassword: ''
		}
	},
	methods: {
		validateAndLogin() {
			console.log(333)
			// if (!checkUserName($("#username").val())) {
			// 	warning("#username", ".error-ident", ".ident-msg", "用户名只能有4-16位（字母，数字）")
			// } else {
			// 	warningResult("#username");
			// }

			// if ($("#username").val() == '') {
			// 	warning("#password", ".error-pass", ".pass-msg", "密码不能为空")
			// } else {
			// 	warningResult("#password");
			// }

			// setTimeout(function showErrorMsg() {
			// 	$(".error-ident, .error-pass").fadeOut();
			// }, 2000)

			// if (countErrors === 0) {
				// $(".error-pass").fadeIn();
				// $(".pass-msg").html("登录成功");
				// if (usernameInput.val() == '学生') {
				// 	$(".confirmation img").attr('src', 'img/login/studen.png');
				// 	setTimeout(() => {
				// 		window.location.href = 'student.html';
				// 	}, 1000);
				// } else {
				// 	$(".confirmation img").attr('src', 'img/login/teacher.png');
				// 	setTimeout(() => {
				// 		window.location.href = 'teacher.html';
				// 	}, 1000);
				// }
				// $(".overlay").show();
				// $(".confirmation").show();
				this.getToken('#loginForm', this.login.username, this.login.password)
					.then(res => {
						let data = JSON.parse(res);
						if(data.code == 0 && data.token) {
							let tokenData = {
								createTime: new Date().getTime(),
								token: data.token
							}
							sessionStorage.setItem('tokenData', JSON.stringify(tokenData));
							window.location.href = './index.html';
						}
					})
					.catch(reason => {
						console.log(reason);
					})
			// }
		},
		validateRegister() {
			if (!checkUserName($("#name").val())) {
				warning("#name", ".error-user", ".name-msg", "用户名只能由4-16个数字或字母组成")
			} else {
				warningResult("#name");
			}

			if (!checkPwd($("#pwd").val())) {
				warning("#pwd", ".error-psw", ".pass-msg", "密码只能由4-12个数字或字母组成")
			} else {
				warningResult("input[name=password]");
			}

			if (!checkRePwd()) {
				warning("#rePwd", ".error-rePsw", ".pass-msg", "两次输入的密码不一致")
			} else {
				warningResult("#rePwd");
			}

			if (countErrors === 0) {
				// alert("注册成功!!!!");
				
				$("#close").click();
			}

			setTimeout(function showErrorMsg() {
				$(".error-psw, .error-rePsw, .error-user").fadeOut();
			}, 2000)
		},
		// 获取token
		getToken(formEl, username, password) {
			return new Promise((resolve, reject) => {
				const xhr = new XMLHttpRequest();
				xhr.open('POST', 'https://api.dsstudio.tech/wx/auth')
				xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				xhr.onreadystatechange = function() {
					if(xhr.readyState == 4 && xhr.status == 200) {
						resolve(xhr.response);
					}
				}
				xhr.send(`username=${username}&password=${password}`);
			})
		},
		// 封装请求操作方法
		request(method, formEl, reqFn) {
			// 获取表单数据
			let form = document.querySelector(formEl);
			let formdata = new FormData(form);
			formdata.set('fn', reqFn);
			return new Promise((resolve, reject) => {
				const xhr = new XMLHttpRequest();
				xhr.open(method, 'https://api.dsstudio.tech/wx/');
				xhr.onreadystatechange = function() {
					if(xhr.readyState == 4 && xhr.status == 200) {
						resolve(xhr.response);
					}
				}
				xhr.send(formdata);
			})
		}
	}
})
